package com.jfs.java.collection;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.builder.EqualsBuilder;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public class HashSetTest {

    public static void main(String[] args) {
        HashSetTest test = new HashSetTest();
        test.test();
    }

    public void test(){
        Set set = new HashSet();
        set.add("a1");
        set.add("a2");
        set.add("a3");
        set.add("a1");
        set.add("");
        set.add(null);
        System.out.println(JSON.toJSONString(set));

        Set set2 = new HashSet();
        App app1 = new App();
        app1.setName("b1");
        set2.add(app1);
        App app2 = new App();
        app2.setName("b1");
        set2.add(app2);
        System.out.println(app1.equals(app2));
        System.out.println(JSON.toJSONString(set2));

        Set<App> set3 = new HashSet<App>();
        App app3 = new App();
        app3.setName("yanshi");
        set3.add(app3);
        set3.add(app3);
        System.out.println("长度："+set3.size()+",内容为："+set3);
    }

    public class App{
        public App(){}
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public int hashCode() {
            int hash = Objects.hash(name);
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            return EqualsBuilder.reflectionEquals(this, obj);
        }
    }

}
